<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Line;
use redis;
use DB;

class LineController extends Controller
{
    private static $handler;
	var $redis_;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->redis_ = new Redis();
        $this->redis_->connect("127.0.0.1", "6379");

	}

	function __destruct()
	{
		if($this->redis_ != null) {
			$this->redis_->close();
		}
	}

    //
    /*
     * 录入
     */
	public function create(Request $request) {
    	$line = Line::create($request->all());
//     	$user = $request;
    	return response()->json($line);
    }
    
    //获取服务器线路列表
    public function get(Request $request) {
    	
    	$where = array();
    	if ($request->input('province')){
    		$province = $request->input('province');
    		$where['province'] = $province;
    	}
    	if ($request->input('city')){
				$city = $request->input('city');
				$where['city'] = $city;
    	}
    	if ($request->input('level')){
    		$level = $request->input('level');
    		$where['level'] = $level;
    	}
    	if ($request->input('carrieroperator')){
    		$carrieroperator = $request->input('carrieroperator');
    		$where['carrieroperator'] = $carrieroperator;
    	}


		$where['enable_flag'] = 1;


		if(!empty($this->redis_->get('line')))
		{
            $line = $this->redis_->get('line');
            $line =  unserialize($line);

        }else{
            echo '查了数据库';
            $line = Line::where($where)->get()->toArray();
            $value = serialize($line);
            $this->redis_->setex('line',10 , $value);
        }


    	$return = array(
    			'code' => 0,
    			'msg' => 'OK',
    			'data' => array(
    					'lines'=>$line
    			)
    	);
        return response()->json($return); 
    	
    }




    public function getlines(Request $request) {

		$where = array();
		if ($request->input('province')){
			$province = $request->input('province');
			$where['province'] = $province;
		}
		if ($request->input('city')){
			$city = $request->input('city');
			$where['city'] = $city;
		}
		if ($request->input('level')){
			$level = $request->input('level');
			$where['level'] = $level;
		}
		if ($request->input('carrieroperator')){
			$carrieroperator = $request->input('carrieroperator');
			$where['carrieroperator'] = $carrieroperator;
		}

        $province = self::getAllPY($province);

//		$where['enable_flag'] = 1;

		$lines = Line::where($where)->where('enable_flag','>',0)->get();
        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'lines'=>$lines
            )
        );

		return response()->json($return);

	}





    public function getIPlistData(Request $request)
    {

        $where['enable_flag'] = 1;


        if($this->redis_->exists('ip_data'))
        {

            $line_data = $this->redis_->get('ip_data');
            $line_data =  unserialize($line_data);

        }else{

            $line_data = Line::where($where)->get(['province','city','niumoip','ip_count','carrieroperator'])->toArray();
            $value = serialize($line_data);
            $this->redis_->setex('ip_data',600 , $value);
        }



        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'lines'=>$line_data
            )
        );
        return response()->json($return);

    }



    /**
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
	 * 客服数据
	 */
	public function getServiceLines(Request $request)
	{

		if(empty($request->input('terminal')) || !$request->has('terminal') || md5('niumokeji') != $request->input('terminal') )
		{
			$return = array(
				'code' => -1,
				'msg' => '信息有误',
				'data' => array()
			);
			return response()->json($return);
		}

		$where = array();
		if ($request->input('province')){
			$province = $request->input('province');
			$where['province'] = $province;
		}
		if ($request->input('city')){
			$city = $request->input('city');
			$where['city'] = $city;
		}
		if ($request->input('level')){
			$level = $request->input('level');
			$where['level'] = $level;
		}
		if ($request->input('carrieroperator')){
			$carrieroperator = $request->input('carrieroperator');
			$where['carrieroperator'] = $carrieroperator;
		}


		$lines = Line::where($where)->where('enable_flag',1)->get(['province','city','carrieroperator','ip as service_address']);

		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' => array(
				'lines'=>$lines
			)
		);
		return response()->json($return);
	}


    public function getServiceLinesV2(Request $request)
    {

        $region =   $request->input('region');
        $province = Line::where(DB::Raw("CONCAT(province,city,niumoip)"),'like',"%$region%" )->where('enable_flag','1')->distinct()->get(['province'])->toArray();


        $city = [];
        foreach($province as $key=>&$value)
        {
            $line = Line::where('enable_flag','1')->where('province',$value['province'])->get(['niumoip as server_address','province','city','carrieroperator as operator'])->toArray();

            $city = array_merge($city,$line);
        }

        $count = 0;
        return self::ShowData($city,$count);
    }


	public function getLinesIpAddress()
	{
		$lines = Line::where('enable_flag','>',0)->get(['ip']);

		$ip = [];
		foreach($lines as $key=>&$line)
		{
			$service_ip =  gethostbyname($line->ip);
			if($service_ip == $line->ip)
			{
				continue;
			}
			$line->ip= $service_ip;
		}

		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' =>$lines
		);

		return response()->json($return);

	}





}
